#include "jsapi.h"
#include "js-engine.h"
#include "desktopClasses.h"
#include "basicElementClass.h"
#include "mainview-parser.h"

static JSBool checkbox_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);

struct JSFunctionSpec checkbox_methods[] = {
    {NULL, NULL}
};

JSClass checkboxClass =
    { "checkbox", JSCLASS_HAS_PRIVATE,
      JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
      JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
      JSCLASS_NO_OPTIONAL_MEMBERS
    };

struct property_parser checkbox_parser[] = {
    /* from basicElement */
    {"cursor", set_property_string_value},
    {"dropTarget", set_property_boolean_value},
    {"enabled", set_property_boolean_value},
    {"height", set_property_integer_value},
    {"hitTest", set_property_string_value},
    {"mask", set_property_string_value},
    {"name", set_property_string_value},
    {"offsetHeight", set_property_integer_value},
    {"offsetWidth", set_property_integer_value},
    {"offsetX", set_property_integer_value},
    {"offsetY", set_property_integer_value},
    {"opacity", set_property_integer_value},
    {"pinX", set_property_integer_value},
    {"pinY", set_property_integer_value},
    {"rotation", set_property_float_value},
    {"tagName", set_property_string_value},
    {"tooltip", set_property_string_value},
    {"width", set_property_integer_value},
    {"visible", set_property_boolean_value},
    {"x", set_property_integer_value},
    {"y", set_property_integer_value},
		
    {"image", set_property_string_value},
    {"checkedDisabledImage", set_property_string_value},
    {"checkedDownImage", set_property_string_value},
    {"checkedOverImage", set_property_string_value},
    {"checkedImage", set_property_string_value},
    {"disabledImage", set_property_string_value},
    {"downImage", set_property_string_value},
    {"overImage", set_property_string_value},
    {"value", set_property_boolean_value},
		
    {ONCLICK, set_property_function_value},
    {ONRCLICK, set_property_function_value},
    {ONDBLCLICK, set_property_function_value},
    {ONRDBLCLICK, set_property_function_value},
    {"ondragdrop", set_property_function_value},
    {"ondragover", set_property_function_value},
    {"onfocusin", set_property_function_value},
    {"onfocusout", set_property_function_value},
    {"onkeydown", set_property_function_value},
    {"onkeypress", set_property_function_value},
    {"onkeyup", set_property_function_value},
    {"onmousedown", set_property_function_value},
    {"onmousemove", set_property_function_value},
    {"onmouseout", set_property_function_value},
    {"onmouseover", set_property_function_value},
    {"onmouseup", set_property_function_value},
    {"onmousewheel", set_property_function_value},
    {NULL, NULL},
};

static JSBool checkbox_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

JSObject *checkboxClass_init(js_engine *engine, JSObject *parent)
{
    JSObject *proto = NULL;
    proto = JS_InitClass(engine->cx, engine->glob, parent,
			 &checkboxClass, checkbox_new,
			 0,NULL,NULL,NULL,NULL);
    g_return_val_if_fail(proto, FALSE);
    g_return_val_if_fail(JS_DefineFunctions(engine->cx, proto, checkbox_methods), FALSE);
    return proto;
}

static gboolean checkbox_toggled_cb(GtkToggleButton *button, gpointer ptr)
{
    desktop_element *element = (desktop_element *)ptr;
    gboolean state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
    jsval value;
    if(state)
	value = JSVAL_TRUE;
    else
	value = JSVAL_FALSE;

    JS_SetProperty(element->engine->cx, element->jsobj, 
		   "value", &value);
    desktop_element_call_handler(element, "onchange");
    return FALSE;
}

void desktop_element_set_view_checkbox(js_engine *engine, desktop_element *element)
{
    jsval caption;
    JS_GetProperty(engine->cx, element->jsobj, "caption", &caption);
    if(JSVAL_IS_STRING(caption)){
	element->view = gtk_check_button_new_with_label(JS_GetStringBytes(JSVAL_TO_STRING(caption)));
    }else{
	element->view = gtk_check_button_new();
    }
    g_signal_connect(element->view, "toggled",
		     G_CALLBACK(checkbox_toggled_cb), element);
    set_element_event_cb(element);
}
